$(document).ready(function() {	
	findAllRole();	
	findAllDeptType();
	$("#deptType").change(function() {
		var dtId = $("#deptType option:selected").val();
		$.ajax({
  			type: "post",
  			data:{"dtId":dtId},
 			url: "admin/userDept_findUserDeptByDtId.action",
  			success:function(data){
  				$("#allDept").empty();
  				$.each(data.userDepts,function(i,n){
  					$("<option value="+n.udId+">"+n.udName+"</option>").appendTo("#allDept");
				});
  			}
  		});
	});
	
	$("#addDept").click(function() {
		if ($("#allDept option:selected").length > 0) {
			$("#allDept option:selected").each(function() {
				$("#selDept").append("<option class='class1' name='selDept' selected value='" + $(this).val() + "'>"
						+ $(this).text() + "</option");
				$(this).remove();
			})
		}
	});
	
	$("#delDept").click(function() {
		if ($("#selDept option:selected").length > 0) {
			$("#selDept option:selected").each(function() {
				$("#allDept").append("<option class='class1' value='" + $(this).val() + "'>"
						+ $(this).text() + "</option");
				$(this).remove();
			})
		}
	});
	
	$("#addLeadDept").click(function() {
		if ($("#selDept option:selected").length > 0) {
			$("#selDept option:selected").each(function() {
				var strHtml="";
				var id="",name=""
				var udId = $(this).val();
				var udName = $(this).text();
				if ($("#leadDept option").length > 0) {
					var t = true;
					$("#leadDept option").each(function(){
						if(udId == $(this).val()){ 
							t = false;
						}
					})
					if(t){
						id=udId;
						name = udName
						strHtml+="<option class='class1' name='leadDept' selected value='" + udId + "'>"
							+ udName + "</option>";
					}
				}else{
					id=udId;
					name = udName
					strHtml+="<option class='class1' name='leadDept' selected value='" + udId + "'>"
						+ udName + "</option>";
				}
				if(id!=""){
					$.ajax( {
						type : "post",
						data : {"udId":id},
						url : "admin/userDept_checkReponsibleDept.action",
						success : function(data){
							//如果部门已经有负责人则返回true
							alert(data)
							if(data=="true")
								alert(name);
							else
								$("#leadDept").append(strHtml);
						}
					});	
				}
			})
		}
	});
	
	$("#delLeadDept").click(function() {
		if ($("#leadDept option:selected").length > 0) {
			$("#leadDept option:selected").each(function() {
					$(this).remove();
			})
		}
	});
	$("#addRole").click(function() {
		if ($("#allRole option:selected").length > 0) {
			$("#allRole option:selected").each(function() {
				$("#selRole").append("<option class='class1' name='selRole' selected value='" + $(this).val() + "'>"
						+ $(this).text() + "</option");
				$(this).remove();
			})
		}
	});
	
	$("#delRole").click(function() {
		if ($("#selRole option:selected").length > 0) {
			$("#selRole option:selected").each(function() {
				$("#allRole").append("<option class='class1' value='" + $(this).val() + "'>"
						+ $(this).text() + "</option");
				$(this).remove();
			})
		}
	});
	
	var uId = $("#uId"),
		uName = $("#uName"),
		uPassword = $("#uPassword"),
		pwd_check = $("#pwd_check"),
		uPhone = $("#uPhone"),
		uTel = $("#uTel"),
		uFax = $("#uFax"),
		uMail = $("#uMail"),
		uRole = $("#uRole"),
		uDept = $("#uDept")
	
	$("#userForm").validate({
		//onsubmit: false,
		//onkeyup: false,
		errorClass: "invalid",
		submitHandler: function(form){   //表单提交句柄,为一回调函数，带一个参数：form  
			var depts,leadDepts,roses;
			var selDept = $("#selDept option");
			if(selDept.length==0){
				alert("所属部门一定要选择！");
				return ;
			}
			depts = new Array(selDept.length);
			for(var i = 0;i < selDept.length; i++ ){
				var value=$(selDept[i]).val();
				depts.push(value);
			}
			var leadDept = $("#leadDept option");
			leadDepts = new Array(leadDept.length);
			for(var i = 0;i < leadDept.length; i++ ){
				var value=$(leadDept[i]).val();
				leadDepts.push(value);
			}
			var selRole = $("#selRole option");
			if(selRole.length==0){
				alert("所属角色一定要选择！");
				return ;
			}
			roles = new Array(selRole.length);
			for(var i = 0;i < selRole.length; i++ ){
				var value=$(selRole[i]).val();
				roles.push(value);
			}
			
       		$.ajax( {
				type : "post",
				data : {"user.UId":uId.val(),"user.UName":uName.val(),
       					"user.UPassword":uPassword.val(),"user.UPhone":uPhone.val(),
       					"user.UTel":uTel.val(), "user.UFax":uFax.val(),"user.UMail":uMail.val(),  
       					"selDepts":depts.toString(),"leadDepts":leadDepts.toString(),
       					"selRoles":roles.toString(),"user.tbUserState.usId":'1001'
						 },
				url : "admin/user_save.action",
				success : function(data){
					 if(data=="true"){
					 	alert("新增用户成功");
					 	window.location.href="/ag/admin/users.jsp";
					 }else{
					 	alert("新增用户失败！");
					 }
				}
			});
			
    	}, 
		rules: {
			'user.UId':{
    			required:true,
    			remote:{
    				url: "admin/user_checkUId.action",
   					type: "post"
    			}
    		},
			'user.UName':{
    			required:true,
    			minlength: 2
    		},
			'user.UPassword':'required',
			'pwd_check': {  
            	required: true,  
            	equalTo: "#uPassword"  
       		 },
       		 'user.UPhone':{
       		 	digits:true,
       		 	minlength:11,
       		 	maxlength:11
       		 },
       		 'user.UMail':'email',
       		 'selRole':{
       			 minlength:1
       		 }
		},
		messages: {
		'user.UId':{
			required:'必须填写',
			remote:'已经被占用'
		},
		'user.UName':{
			required:'必须填写',
			minlength:'不小于两个字',
			remote:'remote'
		},
		'user.UPassword':"必须填写",
		'pwd_check':"密码要一致",
		'user.UPhone':"11位数字",
		'user.UMail':'如：pat@example.com ',
		'selRole':"*"
		}
	});
	
});	
	//获取所有角色
	function findAllRole(){
		$.ajax({
  			type: "post",
 			url: "admin/Role_findAllRole.action",//因为com.ag.admin.action.RoleAction的controller配的是RoleAction
  			success:function(data){
  				$("<option value='0'>请选择</option>").appendTo(".uRole");
  				$.each(data.roles,function(i,n){
  					$("<option value="+n.RId+">"+n.RName+"</option>").appendTo(".uRole");
  					//数据添加到添加用户时选择角色的地方
  					$("<option value="+n.RId+">"+n.RName+"</option>").appendTo("#allRole");
				});
  			}
  		});
	}
	
	//获取所有部门类型
	function findAllDeptType(){
		$.ajax({
  			type: "post",
 			url: "admin/userDept_findAllDeptType.action",
  			success:function(data){
  				$("<option value='0'>请选择</option>").appendTo("#deptType");
  				$.each(data.deptTypes,function(i,n){
  					$("<option value="+n.dtId+">"+n.dtName+"</option>").appendTo("#deptType");
				});
  			}
  		});
	}
	
